package com.coco.seck.service.impl;

import com.coco.common.constant.Constant;
import com.coco.seck.feign.GoodsFeignService;
import com.coco.seck.mapper.SeckillOrderMapper;
import com.coco.seck.pojo.TbGoods;
import com.coco.seck.pojo.TbSeckillGoods;
import com.coco.seck.pojo.TbSeckillOrder;
import com.coco.seck.service.SeckGoodsService;
import com.coco.seck.service.SeckOrderService;
import com.coco.seck.vo.GoodsVo;
import com.coco.seck.vo.SeckOrderGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author HanSama
 * @date 2021年07月20日 21:28
 */
@Service
public class SeckOrderServiceImpl implements SeckOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private GoodsFeignService goodsFeignService;

    @Autowired
    private SeckGoodsService seckGoodsService;

    @Override
    public TbSeckillOrder findOneById(Long id) {
        return seckillOrderMapper.findOneById(id);
    }

    @Override
    public SeckOrderGoods findAliPayOrder(Long id) {

        SeckOrderGoods orderGoods = new SeckOrderGoods();

        TbSeckillOrder seckillOrder = seckillOrderMapper.findOneById(id);

        TbSeckillGoods seckillGoods = seckGoodsService.findById(seckillOrder.getSeckillId());

        GoodsVo data = goodsFeignService.findOne(seckillGoods.getGoodsId()).getData();

        System.out.println(data);

        TbGoods goods = data.getGoods();
        if (Constant.GoodsConstant.SINGLE_SPECIFICATION.equals(goods.getSpecStatus())){
            orderGoods.setSeckGoodsName(data.getGoodsSpec().getTitle());
        }else {
            data.getGoodsSpecsList().forEach(tbGoodsSpecs -> {
                if (seckillGoods.getItemId().equals(tbGoodsSpecs.getId())){
                    orderGoods.setSeckGoodsName(tbGoodsSpecs.getTitle());
                }
            });
        }
        orderGoods.setId(seckillOrder.getId());
        orderGoods.setMoney(seckillOrder.getMoney());


        return orderGoods;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        seckillOrderMapper.updateStatus(id,status);
    }


}
